package com.fire.security.service.impl;


import com.fire.security.domain.CurrentUser;
import com.fire.security.domain.Result;
import com.fire.security.domain.User;
import com.fire.security.mapper.AdminMapper;
import com.fire.security.mapper.UserMapper;
import com.fire.security.service.UserService;
import com.fire.security.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<User> findByConditions(User user) {
        return userMapper.findByConditions(user);
    }

    /*
    * 用户更新
    * */
    @Override
    public Result updateUser(User user) {
        int i= userMapper.updateUser(user);
        if(i>0){
            return Result.ok("更新成功");
        }
        return Result.err(501,"更新失败");

    }
    /*
    * 新增用户
    * */
    @Override
    public Result insertUser(User user) {
        int i= userMapper.insertSelective(user);
        if(i>0){
            if (!stringRedisTemplate.hasKey("todayUser")){
                stringRedisTemplate.opsForValue().set("todayUser", String.valueOf(1), DateUtils.getTimeDifference(new Date()), TimeUnit.SECONDS);
            }else {
                int todayUser = Integer.parseInt(stringRedisTemplate.opsForValue().get("todayUser"));
                stringRedisTemplate.opsForValue().set("todayUser", String.valueOf(todayUser+1), DateUtils.getTimeDifference(new Date()), TimeUnit.SECONDS);
            }
            return Result.ok("添加成功");
        }
        return Result.err(501,"添加失败");
    }


    @Override
    public Result deleteUserById(String id) {
        int i= userMapper.deleteUserById(id);
        if(i>0){
            return Result.ok("删除成功");
        }
        return Result.err(501,"删除失败");
    }

    @Override
    public Result addRead(String id) {
        User user = userMapper.selectById(id);
        int i = userMapper.updateReadNumber(id, String.valueOf(Integer.parseInt(user.getRead())+1));
        if (i == 1){
                if (!stringRedisTemplate.hasKey("todayViewer")){
                    stringRedisTemplate.opsForValue().set("todayViewer", String.valueOf(1), DateUtils.getTimeDifference(new Date()), TimeUnit.SECONDS);
                }else {
                    int todayViewerNumber = Integer.parseInt(stringRedisTemplate.opsForValue().get("todayViewer"));
                    stringRedisTemplate.opsForValue().set("todayViewer", String.valueOf(todayViewerNumber+1), DateUtils.getTimeDifference(new Date()), TimeUnit.SECONDS);
                }
                return Result.ok("添加read成功！");
            }else {
                return Result.err(Result.CODE_ERR_BUSINESS,"添加read失败！");
            }
    }

    @Override
    public CurrentUser findByName(String name) {
        CurrentUser currentUser = adminMapper.selectByName(name);
        return currentUser;
    }

    @Override
    public CurrentUser findUserByName(String name) {
        CurrentUser currentUser = userMapper.selectByName(name);
        return currentUser;
    }

    @Override
    public User findById(String id) {
        User user = userMapper.selectById(id);
        return user;
    }
}
